package cn.com.bluemoon.daps.model.controller;

import cn.com.bluemoon.daps.common.datascope.BmPermission;
import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.model.dto.register.DapModelRegisterDto;
import cn.com.bluemoon.daps.model.entity.DapDataModelTable;
import cn.com.bluemoon.daps.model.service.DapDataModelTableService;
import cn.com.bluemoon.daps.model.vo.register.SearchModelRegister;
import cn.com.bluemoon.dataserver.dto.out.TableApiInfoUsedInfoVO;
import cn.com.bluemoon.dataserver.inter.DataServerModelTableService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/dapModelRegister")
@Api(tags = {"模型表注册"})
public class DapDataModelRegisterController {

    @Resource
    DapDataModelTableService dapDataModelTableService;

    @Resource
    DataServerModelTableService dataServerModelTableService;

    @PostMapping("/list")
    @ApiOperation("注册列表")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = Boolean.class)})
    @BmPermission(value = "model:register:list", name = "注册列表")
    public ResultBean<IPage<DapModelRegisterDto>> modelRegisterList(@RequestBody @Valid SearchModelRegister register){

        IPage<DapModelRegisterDto> versionRegister = dapDataModelTableService.getVersionRegister(register);
        return ResultBean.ok(versionRegister);
    }

    @PostMapping("/do")
    @ApiOperation("注册")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = Boolean.class)})
    @BmPermission(value = "model:table:register", name = "表注册")
    public ResultBean<String> modelTableRegister(@RequestBody List<String> tableIds){

        return dapDataModelTableService.tableRegister(tableIds);
    }

    @PostMapping("/cancel")
    @ApiOperation("取消注册")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = Boolean.class)})
    @BmPermission(value = "table:register:cancel", name = "取消注册")
    public ResultBean<String> tableRegisterCancel(@RequestBody List<String> tableIds){

        return dapDataModelTableService.tableRegisterCancel(tableIds);
    }

    @PostMapping("/relation/api")
    @ApiOperation("注册表关联的服务")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = Boolean.class)})
    @BmPermission(value = "table:relation:api", name = "取消注册")
    public ResultBean<List<String>> relationApi(@RequestBody List<String> tableIds){

        List<DapDataModelTable> tables = dapDataModelTableService.getBaseMapper().selectBatchIds(tableIds);
        List<String> pidList = tables.stream().map(DapDataModelTable::getPId).collect(Collectors.toList());
        cn.com.bluemoon.metadata.common.ResultBean<List<TableApiInfoUsedInfoVO>> result = dataServerModelTableService.getUsedApiInfoByTablePId(pidList);
        if(result.getCode() == 200){
            List<TableApiInfoUsedInfoVO> content = result.getContent();
            if(!content.isEmpty()){
                List<String> collect = content.stream().map(TableApiInfoUsedInfoVO::getTablePId).collect(Collectors.toList());
                List<DapDataModelTable> dapDataModelTables = dapDataModelTableService.getBaseMapper().selectBatchIds(collect);
                Map<String, String> map = dapDataModelTables.stream().collect(Collectors.toMap(DapDataModelTable::getId, DapDataModelTable::getEngName));
                List<String> apiInfo = content.stream().filter(c -> map.containsKey(c.getTablePId())).
                        map(c -> String.format("表%s引用了%s服务", map.get(c.getTablePId()), c.getUsedApiInfoNameList().toString())).collect(Collectors.toList());
                return ResultBean.ok(apiInfo);
            }
            return ResultBean.ok(null);
        }
        return ResultBean.error("数据服务调用失败");
    }
}
